﻿using PropertyChanged;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using WPFTemplateLib.WpfHelpers;

namespace WPFPractice.Windows
{
    /// <summary>
    /// WinColorCompare.xaml 的交互逻辑
    /// </summary>
    public partial class WinColorCompare : Window
    {
        public WinColorCompare()
        {
            InitializeComponent();
            DataContext = new ColorCompareViewModel();
        }
    }

    [AddINotifyPropertyChangedInterface]
    public class ColorCompareViewModel : BindableBase
    {
        #region 绑定

        public SolidColorBrush Color1 { get; set; }
        public SolidColorBrush Color2 { get; set; }
        public bool IsSimilar1 { get; set; }
        public bool IsSimilar2 { get; set; }
        public double Threshold1 { get; set; } = 26;
        public int Threshold2 { get; set; } = 45;

        /// <summary>
        /// 两个颜色的欧几里得距离
        /// </summary>
        public double ColorDistance { get; set; }

        /// <summary>
        /// 两个颜色 RGB 分量差之和
        /// </summary>
        public int ColorSumOfComponentDifferences { get; set; }

        /// <summary>
        /// 是否是连续模式
        /// </summary>
        public bool IsContinuousMode { get; set; } = true;

        #endregion

        #region 命令

        /// <summary>
        /// 比较命令
        /// </summary>
        public ICommand CompareCmd { get; set; }

        /// <summary>
        /// 阈值改变命令
        /// </summary>
        public RelayCommand<string> ThresholdChangedCmd { get; set; }

        /// <summary>
        /// 选择的颜色改变命令
        /// </summary>
        public ICommand SelectedColorChangedCmd { get; set; }

        #endregion

        #region 方法

        public ColorCompareViewModel()
        {
            SetCommandMethod();

            Color1 = new SolidColorBrush(MediaColorHelper.ColorStrToMediaColor("#B9CE9F"));
            Color2 = new SolidColorBrush(MediaColorHelper.ColorStrToMediaColor("#AABD92"));
            OnCompareCmd(null);
        }

        /// <summary>
        /// 命令方法赋值(在构造函数中调用)
        /// </summary>
        private void SetCommandMethod()
        {
            CompareCmd = new RelayCommand(OnCompareCmd);
            ThresholdChangedCmd = new RelayCommand<string>(OnThresholdChangedCmd);
            SelectedColorChangedCmd = new RelayCommand(OnSelectedColorChangedCmd);
        }

        /// <summary>
        /// 比较颜色
        /// </summary>
        /// <param name="obj"></param>
        private void OnCompareCmd(object obj)
        {
            ColorDistance = MediaColorHelper.ColorDistance(Color1.Color, Color2.Color);
            ColorSumOfComponentDifferences = MediaColorHelper.ColorSumOfComponentDifferences(Color1.Color, Color2.Color);
            IsSimilar1 = MediaColorHelper.AreColorsSimilar1(Color1.Color, Color2.Color, Threshold1);
            IsSimilar2 = MediaColorHelper.AreColorsSimilar2(Color1.Color, Color2.Color, Threshold2);
        }

        /// <summary>
        /// 阈值改变
        /// </summary>
        /// <param name="para">哪个阈值</param>
        private void OnThresholdChangedCmd(string para)
        {
            if (para == "1")
            {
                IsSimilar1 = MediaColorHelper.AreColorsSimilar1(Color1.Color, Color2.Color, Threshold1);
            }
            else if (para == "2")
            {
                IsSimilar2 = MediaColorHelper.AreColorsSimilar2(Color1.Color, Color2.Color, Threshold2);
            }
        }

        /// <summary>
        /// 颜色改变
        /// </summary>
        private void OnSelectedColorChangedCmd(object obj)
        {
            if (IsContinuousMode)
            {
                OnCompareCmd(null);
            }
        }

        #endregion
    }
}
